package org.lottery.util;

import android.content.ContentResolver;
import android.content.Intent;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.provider.MediaStore;

public class ImageUtil {

    public static Bitmap loadImage(String imgPath) {

        BitmapFactory.Options options;
        try {
            options = new BitmapFactory.Options();
            options.inSampleSize = 2;
            Bitmap bitmap = BitmapFactory.decodeFile(imgPath, options);
            return bitmap;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getPicturePath(Intent data, ContentResolver cr) {

        String path = "";
        try {
            Uri pickedImage = data.getData();
            String[] filePath = { MediaStore.Images.Media.DATA };
            Cursor cursor = cr.query(pickedImage, filePath, null, null, null);
            cursor.moveToFirst();
            path = cursor.getString(cursor.getColumnIndex(filePath[0]));
            cursor.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return path;
    }

    public static Drawable initRewardPicture(String imagePath, Resources rs) {

    	Drawable draw = null;
        try {
        	if(imagePath==null || imagePath.length()==0)
        		return null;
        	Bitmap img = BitmapFactory.decodeFile(imagePath);
            draw = new BitmapDrawable(rs, img);	
		} catch (Exception e) {
			e.printStackTrace();
		}
        return draw;
    }
}
